var map = require('arr-map')


/**
 * 只能遍历数组
 * @param  {[type]}   arr      [description]
 * @param  {Function} callback [description]
 * @param  {[type]}   thisArg  [description]
 * @return {[type]}            [description]
 */
function arrayMap(arr, callback, thisArg) {
	if (!Array.isArray(arr)) {
	   throw new TypeError('array-unique expects an array.');
	}
	var results = []
	for (var i = 0; i < arr.length; i++) {
		results.push(callback.call(thisArg, arr[i], i, arr))
	}
	
	return results
}


function arrayMapV2(arr, callback, thisArg) {
	if (!Array.isArray(arr)) {
	   throw new TypeError('array-unique expects an array.');
	}

	// 先初始化一个 length长度空数组
	var results = new Array(arr.length)

	for (var i = 0; i < arr.length; i++) {
		results[i]  = callback.call(thisArg, arr[i], i, arr)
	}
	
	return results
}

function f1 (ele) {
  return ele + ele;
}

var f2  = function(ele, i) {
  return i + ele;
}


function test1() {
	console.log( map(['a', 'b', 'c'], f1));
	//=> ['aa', 'bb', 'cc']

	console.log( map(['a', 'b', 'c'], f2));
	//=> ['0a', '1b', '2c']

	console.log('-----------------------\n')

	console.log( arrayMap(['a', 'b', 'c'], f1));
	console.log( arrayMap(['a', 'b', 'c'], f2));



	console.log('-----------------------\n')

	console.log( arrayMapV2(['a', 'b', 'c'], f1));
	console.log( arrayMapV2(['a', 'b', 'c'], f2));
}

function test2() {
	const mapArray = require('map-array');
	const obj = {
		giorgio: 'Bianchi',
		gino: 'Rossi'
	};
	console.log(mapArray(obj, (key, value) => key + ' ' + value));
}


test2() 